package cn.wolfcode.web.controller;


import cn.wolfcode.qo.CustomerReportQuery;
import cn.wolfcode.service.ICustomerReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.MessageUtils;
import cn.wolfcode.util.MessageUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerReportQuery qo, Model model) {
        PageInfo pageInfo = customerReportService.query(qo);
        model.addAttribute("qo", qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customerReport/list";

    }

    @RequestMapping("/bar")
    public String listBar(@ModelAttribute("qo") CustomerReportQuery qo, Model model) {
        List<HashMap> list = customerReportService.listAll(qo);

        //X轴数据
        ArrayList xList = new ArrayList<>();
        //Y轴数据
        ArrayList yList = new ArrayList<>();
        for (HashMap map : list
                ) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }//由于freemarker无法在页面不能直接使用${}直接获取集合对象，在共享到页面前，需要转化为字符串格式
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        return "customerReport/bar";
    }

    @RequestMapping("/pie")
    public String listPie(@ModelAttribute("qo") CustomerReportQuery qo, Model model) {
        List<HashMap> list = customerReportService.listAll(qo);

        //legend数据
        ArrayList legendList = new ArrayList<>();
        //series数据
        ArrayList seriesList = new ArrayList<>();
        for (HashMap map : list
                ) {
            legendList.add(map.get("groupType"));
            HashMap temp = new HashMap();
            temp.put("name", map.get("groupType"));
            temp.put("value", map.get("number"));
            seriesList.add(temp);
        }
        //由于freemarker无法在页面不能直接使用${}直接获取集合对象，在共享到页面前，需要转化为字符串格式
        model.addAttribute("legendList", JSON.toJSONString(legendList));
        model.addAttribute("seriesList", JSON.toJSONString(seriesList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/pie";
    }
}
